//
//  UnitTool.m
//  PPScaleDemo
//
//  Created by 彭思远 on 2021/3/11.
//

#import "UnitTool.h"

@implementation UnitTool

+ (NSString *)weightStrWithDevice:(PPDeviceModel *)deviceMode weightKg:(CGFloat)weightKg andUnitType:(PPUserUnit)unitType{
    if ([UnitTool needShow_2PointWithDevice:deviceMode andUnitType:unitType]) {
        return [NSString stringWithFormat:@"%.2f %@",[UnitTool weightValueWithWightKG:weightKg withUnit:unitType andDevice:deviceMode], [UnitTool getUnitStrWithUnit:unitType]];
    }else{
        return [NSString stringWithFormat:@"%.2f %@",[UnitTool weightValueWithWightKG:weightKg withUnit:unitType andDevice:deviceMode], [UnitTool getUnitStrWithUnit:unitType]];
    }
}

+ (BOOL)needShow_2PointWithDevice:(PPDeviceModel *)deviceMode andUnitType:(PPUserUnit)unitType{
    if (unitType == PPUnitKG) {
        if (deviceMode.deviceAccuracyType == PPDeviceAccuracyTypePoint005) {
            return YES;
        }else{
            return NO;
        }
    }else if (unitType == PPUnitST) {
        return YES;
    }else if (unitType == PPUnitLB) {
        return NO;
    }else{
        return NO;
    }
}

+ (BOOL)needEvenNumberWithDevice:(PPDeviceModel *)deviceMode{
    if (deviceMode.deviceAccuracyType == PPDeviceAccuracyTypePoint005) {
        return NO;
    }else{
        return YES;
    }
}


+ (CGFloat)weightValueWithWightKG:(CGFloat)weightKg  withUnit:(PPUserUnit)unit andDevice:(PPDeviceModel *)deviceMode{

    if (unit==PPUnitKG) {
        return weightKg;
    }else if(unit==PPUnitLB){

        if ([UnitTool needEvenNumberWithDevice:deviceMode]) {
            NSInteger floorWeight = (NSInteger)floorf(weightKg * 10 * 22046 / 10000);
            if (floorWeight % 2 > 0) {
                floorWeight += 1;
            }
            return floorWeight / 10.0;
        }else{
            NSInteger roundWeightInt = (NSInteger)(roundf(weightKg * 100 + 5)/10);
            CGFloat roundWeight= roundWeightInt / 10.0;
            NSInteger floorWeight = (NSInteger)floorf(roundWeight * 10 * 22046 / 10000);
            return floorWeight / 10.0;
        }
    }else if(unit==PPUnitST){
        if ([UnitTool needEvenNumberWithDevice:deviceMode]) {
            CGFloat lb = weightKg * 10 * 22046 / 1000;
            NSInteger st = (NSInteger)floorf(lb / 14);
            return st / 100.0;
        }else{
            NSInteger roundWeightInt = (NSInteger)(roundf(weightKg * 100 + 5)/10);
            CGFloat roundWeight= roundWeightInt / 10.0;
            CGFloat lb = roundWeight * 10 * 22046 / 1000;
            NSInteger st = (NSInteger)floorf(lb / 14);
            return st / 100.0;
        }
 
  
    }else if(unit==PPUnitJin){
        return 2.0 * weightKg;
    }else{
        return weightKg;
    }
}

+ (NSString *)getUnitStrWithUnit:(PPUserUnit)unit{
    if (unit==PPUnitKG) {
        return @"kg";
    }
    else if (unit==PPUnitLB){
        return @"lb";
    }
    else if (unit==PPUnitST){
        return @"st";
    }
    else if (unit==PPUnitJin){
        return @"斤";
    }
    else{
        return @"kg";
    }
}
@end
